//
//  MyInformation.m
//  MINFOBBS
//
//  Created by juvid on 13-12-9.
//  Copyright (c) 2013年 minfo. All rights reserved.
//

#import "EditMyInformation.h"
#import "DetectionSystemVersion.h"
#import "MyInformation.h"
#import "UIColor+expanded.h"
@interface EditMyInformation ()

@end

@implementation EditMyInformation

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
    if ([[DetectionSystemVersion alloc]Version]) {
        self.edgesForExtendedLayout=UIRectEdgeNone;
    }
    arrCell=[[NSArray alloc]initWithObjects:@"昵称",@"姓名",@"性别",@"生日" ,nil];
    arrUser=[[NSMutableArray alloc]initWithObjects:@"",@"",@"男",@"1990-1-1", nil];
    NSUserDefaults *use=[NSUserDefaults standardUserDefaults];
    if ([use objectForKey:@"userInfor"]) {
        arrUser=[[NSMutableArray alloc]initWithObjects:[[use objectForKey:@"userInfor"]objectForKey:@"user"],[[use objectForKey:@"userInfor"]objectForKey:@"name"],[[use objectForKey:@"userInfor"]objectForKey:@"sex"],[[use objectForKey:@"userInfor"]objectForKey:@"born"], nil];
    }
    self.table.layer.cornerRadius=5.0;
    [self BackButton];
    
    // Do any additional setup after loading the view from its nib.
}
-(void)BackButton{
    self.navigationItem.titleView=[DetectionSystemVersion SetBarTitle:@"百事搜"];
    [self.navigationItem setHidesBackButton:YES];
    UIButton *btnBack=[[DetectionSystemVersion alloc]BackButton];
    [btnBack addTarget:self action:@selector(BackReturn:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:btnBack];
    self.navigationItem.leftBarButtonItem=backItem;
    
}
#pragma mark 导航栏按钮事件
-(void)BackReturn:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
//        cell.backgroundColor=[UIColor grayColor];
    }
    cell.indentationLevel=1;
    cell.indentationWidth=15;
    cell.textLabel.text=[arrCell objectAtIndex:indexPath.row];
    cell.textLabel.font=[UIFont systemFontOfSize:15];
     cell.textLabel.textColor=[UIColor colorWithHexString:@"0x474747"];
    if (indexPath.row==0||indexPath.row==1||indexPath.row==3) {
        UITextField  *txt=[[UITextField alloc]initWithFrame:CGRectMake(130, 7, 170, 30)];
        txt.placeholder=@"请输入文字";
        txt.delegate=self;
        txt.text=[arrUser objectAtIndex:indexPath.row];
        txt.font=[UIFont systemFontOfSize:15];
        txt.tag=430+indexPath.row;
        //    txt.backgroundColor=[UIColor redColor];
        txt.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
        cell.accessoryView=txt;
        if (indexPath.row==0) {
            txt.returnKeyType=UIReturnKeyNext;
        }
        else if(indexPath.row==1){
             txt.returnKeyType=UIReturnKeyDone;
        }
        else if (indexPath.row==3) {
             cell.selectionStyle=UITableViewCellSelectionStyleGray;
            txt.enabled=NO;
            txt.tag=429+indexPath.row;
        }

    }
    else {
        UIButton *btn;
        int sex=0;
          NSArray *arr=[[NSArray alloc]initWithObjects:@"男",@"女", nil];
        if ([[arrUser objectAtIndex:indexPath.row] isEqual:@"女"]) {
            sex=1;
        }
        for (int i=0; i<2; i++) {
            btn=[[UIButton alloc]initWithFrame:CGRectMake(120+i*80, 9, 25, 25)];
            UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(155+i*80, 0, 30, 44)];
            lab.font=[UIFont systemFontOfSize:15];
            lab.text=[arr objectAtIndex:i];
            if (i==sex) {
                [btn setImage:[UIImage imageNamed:@"select"] forState:UIControlStateNormal];
                
            }
            else{
                [btn setImage:[UIImage imageNamed:@"unselect"] forState:UIControlStateNormal];
            }
            [btn addTarget:self action:@selector(SelectSex:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag=420+i;
            [cell.contentView addSubview:lab];
            [cell.contentView addSubview:btn];
        }
        arr=Nil;
            }
 
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row==3) {
        [self.view addSubview:self.vieData];
        self.vieData.frame=CGRectMake(0, self.view.frame.size.height, 320, 260);
        [self hideKeyboard];
        [self ShowPick];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)SelectSex:(UIButton *)button{
    [self hideKeyboard];
    [self HidePick];
    if (button.tag==420) {
         UIButton *btn=(UIButton *)[self.view viewWithTag:421];
        [btn setImage:[UIImage imageNamed:@"unselect"] forState:UIControlStateNormal];
        [arrUser setObject:@"男" atIndexedSubscript:2];
    }
    else{
        UIButton *btn=(UIButton *)[self.view viewWithTag:420];
        [btn setImage:[UIImage imageNamed:@"unselect"] forState:UIControlStateNormal];
        [arrUser setObject:@"女" atIndexedSubscript:2];
    }
    [button setImage:[UIImage imageNamed:@"select"] forState:UIControlStateNormal];
}
//文本框代理
-(void)textFieldDidEndEditing:(UITextField *)textField{
    [arrUser setObject:textField.text atIndexedSubscript:textField.tag-430];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [self HidePick];

}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
   
    if (textField.tag==430) {
         UITextField *text=(UITextField *)[self.view viewWithTag:431];
        [text becomeFirstResponder];
    }
    else{
        [textField resignFirstResponder];
    }
    
    return YES;
}
-(void)hideKeyboard{
    UITextField *text;
    for (int i=430; i<432; i++) {
        text=(UITextField *)[self.view viewWithTag:i];
        [text resignFirstResponder];
    }
    
}
- (IBAction)PressSend:(id)sender {
    NSDictionary *dicUser=[[NSDictionary alloc]initWithObjectsAndKeys:[arrUser objectAtIndex:0],@"user",[arrUser objectAtIndex:1],@"name",[arrUser objectAtIndex:2],@"sex",[arrUser objectAtIndex:3],@"born",nil];
    NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
    [user setObject:dicUser forKey:@"userInfor"];
    [self.navigationController popViewControllerAnimated:YES];
   
}

- (IBAction)PressCancle:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)PressTure:(id)sender {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ;
//    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [arrUser setObject:[dateFormatter stringFromDate:self.pickData.date] atIndexedSubscript:3];
    UITextField *text=(UITextField *)[self.view viewWithTag:432];
//    [self.table reloadData];
    text.text=[arrUser objectAtIndex:3];
    [self HidePick];
}

- (IBAction)PressNo:(id)sender {
    [self HidePick];
}
-(void)HidePick{
    [UIView animateWithDuration:0.2
                     animations:^(void) {
                         self.vieData.alpha = 0.0;
                         self.vieData.frame = CGRectMake(0, self.view.frame.size.height, 320, 260);
                     } completion:^(BOOL finished) {
                         
                         [self.vieData removeFromSuperview];
                     }];
    
}
-(void)ShowPick{
    [UIView animateWithDuration:0.2
                     animations:^(void) {
                         self.vieData.alpha = 1.0;
                         self.vieData.frame = CGRectMake(0, self.view.frame.size.height-260, 320, 260);
                     }];
    
}

- (IBAction)PressShowView:(id)sender {
    [self HidePick];
    [self hideKeyboard];
}
@end
